package base.thread.produce_consumer;

import java.util.ArrayList;
import java.util.List;

/**
 * 模拟商店 (进货/销售)
 * 
 * @author fx
 * 
 */
public class Shop {

	private int max = 5;
	private int min = 0;
	private static int i = 0;
	// 产品的容器;达到容器暂停生产,消费到0等待生产
	private static List<Product> list;
	static {
		list = new ArrayList<Product>();
	}

	/**
	 * 生产产品
	 */
	public synchronized void produ() {
		if (list.size() >= max) {
			try {
				System.out.println("--------------生产商品" + i
						+ "时,达到了总数暂停生产-------");
				this.wait();// 进入休眠
			} catch (InterruptedException e) {
				System.out.println(e.toString());
				e = null;
			}
		} // 生产商品
		Product product = new Product();
		product.setName("商品" + i);
		list.add(product);
		System.out.println("生产了商品---->" + product.getName() + "商品总数" + i);
		System.out.println("容器容量" + list.size());
		i++;
		super.notify();

	}

	/**
	 * 消费产品
	 * 
	 * @return
	 */
	public synchronized void cousu() {
		if (list.size() <= min) {// 消费完时,挂起
			System.out
					.println("+++++++++++++++++++++++商品消费完了.等待+++++++++++++++=");
			try {
				this.wait();
			} catch (InterruptedException e) {
				System.out.println(e.toString());
				e = null;
			}
		}
		Product product = list.get(0);
		list.remove(0);
		System.out.println("消费了获得了商品-->" + product.getName());
		System.out.println("容器容量" + list.size());
		super.notify();

	}
}
